<template>
    <div>
        <van-form style="margin-top: 10%" @submit="onSubmit">
            <van-field
                    readonly
                    clickable
                    name="picker"
                    v-model="userId"
                    label="羊毛户"
                    placeholder="点击选择羊毛户"
                    @click="ShowPicker = true"
                    autocomplete="off"
                    required
            />
            <van-popup v-model:show="ShowPicker" position="bottom">
                <van-picker
                        show-toolbar
                        :columns="allUser"
                        @confirm="userOnConfirm"
                        @cancel="userreset"
                        cancel-button-text="重置"
                />
            </van-popup>


            <van-field
                    readonly
                    clickable
                    name="picker"
                    v-model="woolTaskId"
                    label="羊毛APP"
                    placeholder="点击选择羊毛APP"
                    @click="appShowPicker = true"
                    autocomplete="off"
                    required
            />
            <van-popup v-model:show="appShowPicker" position="bottom">
                <van-picker
                        show-toolbar
                        :columns="allApp"
                        @confirm="woolTaskOnConfirm"
                        @cancel="woolTaskreset"
                        cancel-button-text="重置"
                />
            </van-popup>


            <van-field
                    readonly
                    clickable
                    name="picker"
                    v-model="appDetailsId"
                    label="该APP任务"
                    placeholder="点击选择该APP任务"
                    @click="appDetailsShow"
                    autocomplete="off"
                    required
            />
            <van-popup v-model:show="appDetailsShowPicker" position="bottom">
                <van-picker
                        show-toolbar
                        :columns="appAppDetails"
                        @confirm="appAppDetailsOnConfirm"
                        @cancel="appAppDetailsreset"
                        cancel-button-text="重置"
                />
            </van-popup>

            <van-field
                    v-model="token"
                    name="token"
                    label="请输入token"
                    placeholder="请输入任务token"
                    :rules="[{ required: true, message: '请输入token' }]"
            />
            <div style="margin: 16px;">
                <van-button round block type="primary" native-type="submit">提交</van-button>
            </div>


        </van-form>

    </div>


</template>

<script>
    import myInfo from '@/api/myInfo';
    import UserDoTask from '@/api/UserDoTask';
    import {Toast} from "vant";

    export default {
        name: "index",
        data() {
            return {
                appId: '',
                userId: '',
                ShowPicker: false,
                allUser: [],
                woolTaskId: '',
                appShowPicker: false,
                allApp: [],
                appDetailsId: '',
                appDetailsShowPicker: false,
                appAppDetails: [],
                token: '',
                allAppToDo:[],
                allAppDetails:[],
            }
        },
        created() {

            this.getAllUser();
            this.getAllApp();
            this.getAllAppDetails();

        },
        mounted() {

        },
        methods: {
            getAllUser() {
                myInfo.getAllUser().then(response => {
                    if (response.code === 200) {
                        let users = response.data;
                        let result = [];
                        for (let item of users) {
                            result.push('用户:' + item.userId + '-' + item.userName)
                        }
                        this.allUser = result;
                    } else {
                        Toast(response.message + '');
                    }

                }).catch(error => {
                    console.log(error);
                });
            },
            getAllApp() {
                UserDoTask.getAllApp().then(response => {
                    if (response.code === 200) {
                        let apps = response.data;
                        this.allAppToDo = apps;
                        let result = [];
                        for (let item of apps) {
                            result.push('软件:' + item.id + '-' + item.woolName)
                        }
                        this.allApp = result;
                    } else {
                        Toast(response.message + '');
                    }

                }).catch(error => {
                    console.log(error);
                });
            },
            getAllAppDetails() {
                UserDoTask.getAllAppDetails().then(response => {
                    if (response.code === 200) {
                        let appDetails = response.data;
                        this.allAppDetails = response.data;
                        let result = [];
                        for (let item of appDetails) {
                            result.push('任务:' + item.woolId + '-' + item.taskName);
                        }
                        this.appAppDetails = result;

                    } else {
                        Toast(response.message + '');
                    }

                }).catch(error => {
                    console.log(error);
                });
            },
            appDetailsShow(){
                this.appDetailsShowPicker = true;
                let appids = this.allAppToDo;
                let oneid = this.woolTaskId;//软件：1-九章头条
                if (oneid != '') {
                    let string = oneid.split(':')[1].split('-')[0];
                    let result = [];
                    appids.forEach(item => {
                        if (item.id + '' == string) {
                            for (let item1 of this.allAppDetails) {
                                if (item1.woolId == string) {
                                    result.push('任务:' + item1.sort + '-' + item1.taskName);
                                }
                            }
                        }
                    });
                    this.appAppDetails = [];
                    this.appAppDetails = result;
                } else {
                    this.appAppDetails = [];
                }
            },

            userOnConfirm(value) {
                this.userId = value;
                this.ShowPicker = false;
            },
            userreset() {
                this.userId = '';
                this.ShowPicker = false;
            },

            woolTaskOnConfirm(value) {
                this.woolTaskId = value;
                this.appShowPicker = false;
            },
            woolTaskreset() {
                this.woolTaskId = '';
                this.appShowPicker = false;
            },


            appAppDetailsOnConfirm(value) {
                this.appDetailsId = value;
                this.appDetailsShowPicker = false;
            },
            appAppDetailsreset() {
                this.appDetailsId = '';
                this.appDetailsShowPicker = false;
            },

            onSubmit(values) {
                UserDoTask.add({
                        'userId': this.userId,
                        'woolTaskId': this.woolTaskId,
                        'woolTaskDetailId': this.appDetailsId,
                        'token': values.token,
                    }).then(response => {
                }).catch(error => {
                    console.log(error);
                })
            },

        },
    }
</script>

<style scoped>

</style>
